;;; GNU Guix --- Functional package management for GNU
;;;
;;; Copyright © 2022 jgart <jgart@dismail.de>
;;; Copyright © 2022 Evan Straw <evan.straw99@gmail.com>
;;;
;;; This file is not part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guixrus packages olive-editor)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix build-system cmake)
  #:use-module (guix build-system gnu)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix utils)
  #:use-module (guix build utils)
  #:use-module (guix gexp)
  #:use-module (guix packages)
  #:use-module (gnu packages)
  #:use-module (gnu packages audio)
  #:use-module (gnu packages autotools)
  #:use-module (gnu packages ghostscript)
  #:use-module (gnu packages qt)
  #:use-module (gnu packages gl)
  #:use-module (gnu packages xml)
  #:use-module (gnu packages serialization)
  #:use-module (gnu packages python)
  #:use-module (gnu packages python-xyz)
  #:use-module (gnu packages version-control)
  #:use-module (gnu packages graphics)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages image-processing)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages video))

(define-public pystring
  (package
    (name "pystring")
    (version "1.1.3")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/imageworks/pystring")
                    (commit (string-append "v" version))))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "1w31pjiyshqgk6zd6m3ab3xfgb0ribi77r6fwrry2aw8w1adjknf"))))
    (build-system gnu-build-system)
    (arguments
     `(#:test-target "test"
       #:phases
       (modify-phases %standard-phases
         ;; pystring does not have a configure script
         (delete 'configure)
         ;; Makefile attempts to install to /usr/lib; change this to the
         ;; proper output path
         (add-before 'install 'fix-install
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (lib (string-append out "/lib")))
               (substitute* "Makefile"
                 (("LIBDIR = /usr/lib")
                  (string-append "LIBDIR = " lib)))
               (mkdir-p lib)
               #t)))
         ;; Makefile does not install the header files for the library;
         ;; install them to an "include" directory under the proper output
         ;; path
         (add-after 'install 'install-header
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (inc (string-append out "/include/pystring")))
               (mkdir-p inc)
               (copy-file "pystring.h"
                          (string-append inc "/pystring.h"))))))))
    (native-inputs
      (list libtool))
    (home-page "https://github.com/imageworks/pystring")
    (synopsis "C++ functions matching the interface and behavior of Python
string methods")
    (description "Pystring is a collection of C++ functions which match the
interface and behavior of python's string class methods using std::string.
Implemented in C++, it does not require or make use of a python interpreter.
It provides convenience and familiarity for common string operations not
included in the standard C++ library.  It's also useful in environments where
both C++ and python are used.")
    (license license:bsd-3)))

(define-public opencolorio-2
  (package
    (name "opencolorio")
    (version "2.0.0")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/AcademySoftwareFoundation/OpenColorIO")
             (commit (string-append "v" version))))
       (sha256
        (base32 "0888fca8wa5zdc6f7lmh1wi7ljw75ql0rlzaslk2zffd08ij0s38"))
       (file-name (git-file-name name version))))
    (build-system cmake-build-system)
    (arguments
     `(#:configure-flags
       (list "-DOCIO_INSTALL_EXT_PACKAGES=NONE"
             (string-append "-Dpystring_ROOT="
                            (assoc-ref %build-inputs "pystring")))))
    (native-inputs
      (list git pkg-config python))
    (inputs
      (list lcms
            openexr
            tinyxml
            expat
            pybind11
            yaml-cpp
            ilmbase
            pystring))
    (home-page "https://opencolorio.org")
    (synopsis "Color management for visual effects and animation")
    (description
     "OpenColorIO, or OCIO, is a complete color management solution geared
towards motion picture production, with an emphasis on visual effects and
computer animation.  It provides a straightforward and consistent user
experience across all supporting applications while allowing for sophisticated
back-end configuration options suitable for high-end production usage.

OCIO is compatible with the @acronym{ACES, Academy Color Encoding
Specification} and is @acronym{LUT, look-up table}-format agnostic, supporting
many popular formats.")
    (license (list license:zlib         ; src/OpenColorIO/md5
                   license:bsd-3))))    ; the rest

(define-public openexr-3
  (package
    (name "openexr")
    (version "3.0.4")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/AcademySoftwareFoundation/openexr")
                    (commit (string-append "v" version))))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "1pcd3rqdkl07fscnkxbxpy0h0qdzy5w5dl5k5mk36k6nizy2z8gx"))))
    (build-system cmake-build-system)
    (arguments
     `(#:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'fix-test-tmpdir
             (lambda _
               (let* ((new-tmpdir (string-append (getcwd) "/test-tmp"))
                      (test-dir (string-append (getcwd) "/src/test/"))
                      (get-tmpdir-h (lambda (dir) (string-append test-dir dir "/tmpDir.h")))
                      (replace-files (list (get-tmpdir-h "OpenEXRFuzzTest")
                                           (get-tmpdir-h "OpenEXRTest")
                                           (get-tmpdir-h "OpenEXRUtilTest"))))
                 (mkdir-p new-tmpdir)
                 (map (lambda (file)
                        (substitute* file
                          (("/var/tmp") new-tmpdir)))
                      replace-files)))))))
    (inputs
      (list zlib imath))
    (home-page "https://www.openexr.com")
    (synopsis "High-dynamic range file format library")
    (description "OpenEXR is a high dynamic-range (HDR) image file format developed for use in
computer imaging applications.  The IlmImf C++ libraries support storage of the \"EXR\" file
format for storing 16-bit floating-point images.")
    (license license:bsd-3)))

(define-public optional-lite
  (package
    (name "optional-lite")
    (version "3.4.0")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/martinmoene/optional-lite")
                    (commit (string-append "v" version))))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "17g8jcwyc3skf8nr68yr0mbp4q2hankgidgdgjfkyl03f5ysy64v"))))
    (build-system cmake-build-system)
    (home-page "https://github.com/martinmoene/optional-lite")
    (synopsis "C++17-like optional type for C++98, C++11, and later")
    (description
"optional lite is a single-file header-only library to represent optional
(nullable) objects and pass them by value.  The library aims to provide
a C++17-like optional for use with C++98 and later.")
    (license license:boost1.0)))

(define-public cpp11-any
  (let ((version "0.0.0")
	(revision "1")
	(commit "f67bd5f8bbf7eb628bf38206d4ac5cb22438e6bb"))
    (package
      (name "cpp11-any")
      (version (git-version version revision commit))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url "https://github.com/thelink2012/any")
                      (commit commit)))
                (file-name (git-file-name name version))
                (sha256
                 (base32
                  "1ch6aq363dqx6bx7yjg4lkq346sy2acw1fnamg4jdqs4z8rqz9jz"))))
      (build-system cmake-build-system)
      (home-page "https://github.com/thelink2012/any")
      (synopsis "Implementation of std::experimental::any")
      (description
"This is a implementation of N4562 std::experimental::any (merged into
C++17) for C++11 compilers.  It contains a small object optimization
for objects with a size of up to 2 words (such as int, float and
std::shared_ptr).  Storing those objects in the container will not trigger
a dynamic allocation.")
      (license license:boost1.0))))

(define-public olive
  (let ((revision "2")
	    (commit "efa74c72a80522fdd85a56e7dee13e341b73de99"))
    (package
      (name "olive")
      (version (git-version "0.2.0" revision commit))
      (source (origin
		(method git-fetch)
		(uri (git-reference
		      (url "https://github.com/olive-editor/olive")
		      (commit commit)))
		(file-name (git-file-name name version))
		(sha256
		 (base32
		  "00zv1h9acf3z76ivy04272r4f6rv0yip4bx1vs5znajffnyaj0jm"))))
      (build-system cmake-build-system)
      (arguments
       `(#:configure-flags '("-DCMAKE_CXX_FLAGS=-Wno-error=shadow")))
      (inputs
        (list qtbase-5
		      qtmultimedia
		      qttools
		      qtsvg
		      ffmpeg
		      opencolorio-2
		      openimageio
              portaudio
              zlib
              imath
		      openexr-3
              ilmbase))
      (native-inputs
        (list pkg-config))
      (home-page "https://olivevideoeditor.org")
      (synopsis "Free non-linear video editor")
      (description
"@code{olive} is a free and open source non-linear video editor.")
      (license license:gpl3+))))

;; TODO let's automate tracking nightlies
(define-public olive-5dd6f9d
  (let ((revision "0")
	    (commit "5dd6f9d0ce923ef96147525f4aced2c67e71d184"))
    (package (inherit olive)
      (name "olive-5dd6f9d")
      (version (git-version "0.2.0" revision commit))
      (source (origin
		(method git-fetch)
		(uri (git-reference
		      (url "https://github.com/olive-editor/olive")
		      (commit commit)))
		(file-name (git-file-name name version))
		(sha256
		 (base32
		  "0s04z38r2nvjjazy3fd1vxh2wx57hgdfj7mfjgp03imdmijba86c"))))
      (build-system cmake-build-system)
      (arguments
       `(,@(package-arguments olive)
         #:tests? #f)) ; fixme
;; starting phase `check'
;; Running tests...
;; /gnu/store/zga679c4nldah9l8dhd5a4hdy820hcyf-cmake-minimal-3.21.4/bin/ctest --force-new-ctest-process 
;; Test project /tmp/guix-build-olive-0.2.0-3.5dd6f9d.drv-0/build
    ;; Start 1: compositing-tests
;; 1/5 Test #1: compositing-tests ................   Passed    0.05 sec
    ;; Start 2: common-tests
;; 2/5 Test #2: common-tests .....................   Passed    0.05 sec
    ;; Start 3: rational-tests
;; 3/5 Test #3: rational-tests ...................   Passed    0.05 sec
    ;; Start 4: timerange-tests
;; 4/5 Test #4: timerange-tests ..................   Passed    0.05 sec
    ;; Start 5: timeline-tests
;; 5/5 Test #5: timeline-tests ...................Subprocess aborted***Exception:   0.05 sec
;; Failed to create destination directory "/homeless-shelter/.cache/ocioconf"
;; Extracting default OCIO config to "/homeless-shelter/.cache/ocioconf"
;; terminate called after throwing an instance of 'OpenColorIO_v2_0::Exception'
  ;; what():  Error could not read '/homeless-shelter/.cache/ocioconf/config.ocio' OCIO profile.
;; 
;; 
;; 80% tests passed, 1 tests failed out of 5
;; 
;; Total Test time (real) =   0.25 sec
;; 
;; The following tests FAILED:
	  ;; 5 - timeline-tests (Subprocess aborted)
;; Errors while running CTest
;; make: *** [Makefile:139: test] Error 8
      (synopsis "trackviewitem: fixed bug")))) ; TODO automate getting git commit message

